



*Evidence of long term use

use "$data_out/provider_propensity.dta", clear




//create variable 
label var ed_prescription "Prescription in ED"
label var prov7 "7 Different Prescribers by 1 Year"
label var day_180 "Probability of 180 Days of Suppply within 1 Year"
label var scripts_365 "Number of Opioid Prescriptions within 1 year"

//run regressions and store results

	foreach outcome in scripts_365 prov7  day_180  {
eststo `outcome'_C:	ivreghdfe `outcome' $spec1
estadd ysumm, replace 

eststo `outcome'_Q:	ivreghdfe `outcome' $spec2
estadd ysumm, replace 
}

//build tables
#delimit ;

	//word
esttab scripts_365_C prov7_C  day_180_C using "opioid_use.rtf" , 
	replace label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats(ymean N ,
	fmt(a2 %-9.0fc)
	label("Sample Mean" "Observations"))
		keep(ed_prescription)
		title(IV Results for Probability of Long-Term Opioid Use - Prescribing Rate Instrument);
		
		
		esttab scripts_365_Q  prov7_Q  day_180_Q using "opioid_use.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats(ymean N ,
	fmt(a2 %-9.0fc)
	label("Sample Mean" "Observations"))
		keep(ed_prescription)
		title(IV Results for Probability of Long-Term Opioid Use - 4th vs 1st Quartile Instrument);
		

 
#delimit cr
	
*Random drug tests
	
cap confirm file "$data_out/drug_tests.dta"
if _rc!=0 { 

do "$Outcomes/Drug_Tests"
}
else use "$data_out/drug_tests.dta", clear

label var ed_prescription "Prescription in ED"
label var ua_test_positive "Probability of Drug Test Failure within 1 Year"
label var ua_opiate_illegal "Probability of Drug Test Failure Due to Opioids without a Prescription"
label var ua_opiate_prescribed "Probability of Failure Due to Prescribed Opioids"
label var ua_heroin "Heroin"
label var ua_MJ "Marijuana"
label var ua_benzo "Benzos"
label var ua_other "Other Drugs"

foreach outcome of varlist ua_test_positive ua_opiate_prescribed ua_opiate_illegal ua_heroine ua_MJ ua_benzo ua_other {
eststo `outcome'_C:	ivreghdfe `outcome' $spec1
estadd ysumm, replace

eststo `outcome'_Q:	ivreghdfe `outcome' $spec2
estadd ysumm, replace
}


#delimit ;

	//word
esttab  ua_test_positive_C   ua_opiate_prescribed_C ua_opiate_illegal_C  ua_heroine_C ua_MJ_C ua_benzo_C ua_other_C using "drug_tests.rtf" , 
	replace label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se()
	stats(ymean N ,
	fmt(a2 %-9.0fc)
	label("Sample Mean" "Observations"))
	keep(ed_prescription)
		title(IV Results for Probability of Drug Test Failure - Continuous Instrument);
		
		
		esttab ua_test_positive_Q   ua_opiate_prescribed_Q ua_opiate_illegal_Q  ua_heroine_Q ua_MJ_Q ua_benzo_Q ua_other_Q  using "drug_tests.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats(ymean N ,
	fmt(a2 %-9.0fc)
	label("Sample Mean" "Observations"))
		keep(ed_prescription)
		title(IV Results for Probability of Drug Test Failure - 4th vs 1st Quartile Instrument);
		

#delimit cr
